# coding: utf-8

from wtforms import (
    StringField, FloatField, SubmitField, DecimalField, SelectField
)
from wtforms.validators import Optional, DataRequired
from wtforms_components import read_only

from app.op.constant import DemandCate, DemandColor, DemandType, AreaType
from app.op.models import Area
from ..forms import WphForm, NonValidatingSelectField, NotChoices


class WaterFactorFrom(WphForm):
    """
    城市灌水系数表单
    """
    name = StringField('城市', validators=[Optional()])
    water_factor = FloatField('浇水系数', default=0)
    submit = SubmitField('保存')

    def __init__(self, *args, **kwargs):
        super(WaterFactorFrom, self).__init__(*args, **kwargs)
        read_only(self.name)


class WaterPreviewForm(WphForm):
    """
    created by chenhj at 2018/4/26
    浇水系数结果预览表单
    """
    cate = NonValidatingSelectField(
        '需求分类', coerce=int,
        choices=[
            i for i in DemandCate.to_options()
            if i[0] != DemandCate.MAINTAIN.value
        ],
        validators=[DataRequired()],
    )
    size = DecimalField('面积(平米)', validators=[DataRequired()])
    color = NonValidatingSelectField(
        '颜色', coerce=int,
        choices=DemandColor.to_options(),
        validators=[DataRequired()],
    )
    span = StringField('间距', validators=[DataRequired()])
    type = NonValidatingSelectField(
        '屏幕类型', coerce=int,
        choices=DemandType.to_options(),
        validators=[DataRequired()],
    )
    city = SelectField('城市', validators=[DataRequired()])

    def __init__(self, *args, **kwargs):
        super(WaterPreviewForm, self).__init__(*args, **kwargs)

        self.city.choices = NotChoices + \
            [
                (area.name, area.name)
                for area in Area.query.filter_by(
                    type=AreaType.CITY.value
                ).order_by(Area.id.asc())
            ]
        self.city.default = NotChoices[0][0]
